import Vue from "vue";

import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";
import { isLogin } from "../utils/tools";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: HomeView,
  },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  },

  // {
  //   path: "/home",
  //   name: "Home",
  //   component: () => import("../views/Home.vue"),
  // },
  {
    path: "/list/:id?",
    name: "List",
    component: () => import("../views/List.vue"),
  },
  {
    path: "/detail",
    name: "Detail",
    component: () => import("../views/Detail.vue"),
    meta: {
      hideTabBar: true,
    },
  },
  {
    path: "/car",
    name: "Car",
    component: () => import("../views/Car.vue"),
    meta: {
      needLogin: true,
    },
  },
  {
    path: "/user",
    name: "User",
    component: () => import("../views/User.vue"),
    meta: {
      needLogin: true,
    },
  },
  {
    path: "/reg",
    name: "Reg",
    component: () => import("../views/RegView.vue"),
    meta: {
      hideTabBar: true,
    },
  },
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/LoginView.vue"),
    meta: {
      hideTabBar: true,
    },
  },
  {
    path: "/changePsw",
    name: "ChangePsw",
    component: () => import("../views/ChangePsw"),
    meta: {
      hideTabBar: true,
    },
  },
  {
    path: "/modifyInfo",
    name: "ModifyInfo",
    component: () => import("../views/ModifyInfo"),
    meta: {
      hideTabBar: true,
    },
  },
  {
    path: "/userOrders",
    name: "UserOrders",
    component: () => import("../views/UserOrders.vue"),
    meta: {
      hideTabBar: true,
    },
  },
  {
    path: "/orderDetail",
    name: "OrderDetail",
    component: () => import("../views/OrderDetail.vue"),
    meta: {
      hideTabBar: true,
    },
  },
  {
    path: "/address",
    name: "Address",
    component: () => import("../views/addAddress.vue"),
  },
];

const router = new VueRouter({
  routes,
});
// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.needLogin) {
    if (isLogin()) {
      next();
    } else {
      next({
        name: "Login",
      });
    }
  } else {
    next();
  }
});
export default router;
